<?php
/**
 * Created by PhpStorm.
 * User: luoqinghui
 * Date: 2020/7/24
 * Time: 上午17:40
 */

namespace App\Http\Controllers\Api\Device;

use App\Http\Controllers\Controller;
use App\Modules\Index\Services\diningTimeService;
use Illuminate\Http\Request;
use Config;
use App\Modules\Index\Services\GlucoseService;
use App\Modules\Index\Services\InsulinService;
use App\Modules\Task\Services\TaskService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
/**
 * 设备数据
 * Class HomeController
 * @package App\Http\Controllers\Api\Home
 */
class ThirdDeviceController extends Controller
{
    /**
     * 三诺设备数据接口
     */
    public function sinoData(Request $request){
        $str = $request->getContent();
        Log::useDailyFiles(storage_path('logs/sino/sino.log'));
        Log::info($str);
        $sinokey = Config::get('app.sinokey');
        if($str){
            $param = \GuzzleHttp\json_decode($str);
            //验证签名
            $timestamp = $param->head->timestamp;
            $randomnumber = $param->head->randomnumber;

            $signature = md5($timestamp.$sinokey.$randomnumber);

            if($signature != $param->head->signature){
                $res = array(
                    'statusCode' => '01',
                    'desc'       => '签名错误'
                );
                return json_encode($res);
            }
            $uid =  DB::table('device')->where('device_code',$param->msgdata->devicesn)->value('bundled_user_id');
            $glu = $param->msgdata->result;
            $device_code = $param->msgdata->devicesn;
            $meals = ThirdDeviceController::userMeals($uid,$param->msgdata->testtime);
            $state = $param->msgdata->aimstatus;
            $test_at = $param->msgdata->testtime;
            if($param->msgdata->aimstatus == 1) $state = 2;
            if($param->msgdata->aimstatus == 3) $state = 1;
            if($param->msgdata->aimstatus == 5) $state = 3;

            if(empty($uid)){
                $res = array(
                    'statusCode' => '02',
                    'desc'       => '设备未绑定用户'
                );
                return json_encode($res);
            }

            if(empty($glu) || empty($device_code) || empty($meals) || empty($state) || empty($test_at)){
                $res = array(
                    'statusCode' => '03',
                    'desc'       => '缺少参数'
                );
                return json_encode($res);
            }

            //添加血糖
            $dataAddArr = [
                'user_id'         => $uid,
                'glu'         => $glu,
                'device_code'          => $device_code,
                'meals'   => $meals,
                'test_at'       =>$test_at,
                'state'       =>$state,
                'type'       =>2
            ];

            $result = GlucoseService::addGlu($dataAddArr);
            if ( !$result ) {
                $res = array(
                    'statusCode' => '04',
                    'desc'       => '添加失败'
                );
                return json_encode($res);
            }

            $merge['user_id'] = $uid;
            $merge['meals'] = $meals;
            $merge['time'] = $test_at;
            $merge['glucose'] = $glu;
            $merge['sign'] = 1;

            if ($state == 1) $merge['type'] = '正常';
            else if ($state == 2) $merge['type'] = '偏低';
            else if ($state == 3) $merge['type'] = '偏高';

            $merge['created_at'] = date('Y-m-d H:i:s');
            $merge['updated_at'] = date('Y-m-d H:i:s');
            $result = InsulinService::addMergeInsulin($merge);

            if($meals == 7){
                $taskObj = new TaskService();
                $taskObj->taskMeasurement($uid);
            }

            $res = array(
                'statusCode' => '00',
                'desc'       => 'success'
            );
            return json_encode($res);

        }else{
            $res = array(
                'statusCode' => '05',
                'desc'       => '没有数据'
            );
            return json_encode($res);
        }



    }

    /**
     * 判断餐别
     */
    public function userMeals($user_id,$time){
        //查询用户的基本配置
//        $time = '2020-09-01 21:25:15';
        $date =date('H:i:s',strtotime($time));//当前时间
        $result = diningTimeService::mealsTime($user_id);
        $meals = $result['mealsconf'];

        //区间范围判断
        if(strtotime($meals[0]['start_time'])< strtotime($date) && strtotime($date)  < strtotime($meals['0']['end_time']) ){
            if(floor((strtotime($meals['0']['start_time']) + strtotime($meals['0']['end_time']))/strtotime($date)) > 1 ) return 1;
            else return 2;
        }

        if(strtotime($meals[0]['end_time'])< strtotime($date) && strtotime($date)  < strtotime($meals['1']['start_time']) ){
            if(floor((strtotime($meals['0']['end_time']) + strtotime($meals['1']['start_time']))/strtotime($date)) > 1 ) return 2;
            else return 3;
        }

        if(strtotime($meals[1]['start_time'])< strtotime($date) && strtotime($date)  < strtotime($meals['1']['end_time']) ){
            if(floor((strtotime($meals['1']['start_time']) + strtotime($meals['1']['end_time']))/strtotime($date)) > 1 ) return 3;
            else return 4;
        }

        if(strtotime($meals[1]['end_time'])< strtotime($date) && strtotime($date)  < strtotime($meals['2']['start_time']) ){
            if(floor((strtotime($meals['1']['end_time']) + strtotime($meals['2']['start_time']))/strtotime($date)) > 1 ) return 4;
            else return 5;
        }

        if(strtotime($meals[2]['start_time'])< strtotime($date) && strtotime($date)  < strtotime($meals['2']['end_time']) ){
            if(floor((strtotime($meals['2']['start_time']) + strtotime($meals['2']['end_time']))/strtotime($date)) > 1 ) return 5;
            else return 6;
        }

        if(strtotime($meals[2]['end_time'])< strtotime($date) && strtotime($date)  < strtotime($meals['3']['start_time']) ){
            if(floor((strtotime($meals['2']['end_time']) + strtotime($meals['3']['start_time']))/strtotime($date)) > 1 ) return 6;
            else return 7;
        }
        if($meals['3']['end_time'] == '00:00') $meals['3']['end_time'] = '23:59';

        if(strtotime($meals[3]['start_time'])< strtotime($date) && strtotime($date)  < strtotime($meals['3']['end_time']) ){
             return 7;
        }

        return 1;

    }


}
























